//
// Created by bianzheng on 2022/6/14.
//

#include <string>
#include <vector>
#include <spdlog/spdlog.h>

#include "StringUtil.hpp"
#include "ConvertRatings.hpp"
#include "FileOut.hpp"

using namespace Rec2MIPS;

void ReadFile(const char *input_fname, std::vector<RatingString> &rating_str_l) {
    std::ifstream in(input_fname);
    std::string line;

    const int report_every_ = 1000000;
    int process_idx = 0;
    getline(in, line); //skip the first line, it is the head
    while (getline(in, line)) // line中不包括每行的换行符
    {
        if (process_idx++ % report_every_ == 0) {
            spdlog::info("read idx movielens-27m {}", process_idx);
        }
        std::vector<std::string> arr_str = Rec2MIPS::split(line, ',');
        std::string user_id = arr_str[0];
        std::string item_id = arr_str[1];
        std::string rating = arr_str[2];
        rating_str_l.emplace_back(user_id, item_id, rating);
    }
    in.close();
}

using namespace std;


int main(int argc, char **argv) {
    if (argc != 3) {
        spdlog::info("input filename, output filename");
        return 0;
    }
    char *input_fname = argv[1];
    char *output_fname = argv[2];
    spdlog::info("input filename {}, output filename {}", input_fname, output_fname);

    std::vector<RatingString> rating_str_l;
    ReadFile(input_fname, rating_str_l);

    std::vector<RatingNum> rating_num_l;
    pair<int, int> data_stat = ConvertRatings(rating_str_l, rating_num_l);

    spdlog::info("MovieLens27m total n_user {}, total n_item {}", data_stat.first, data_stat.second);

    WriteFile(output_fname, "MovieLens27m", rating_num_l);
    return 0;
}